<div class="ui-corner-all package">
    <?php  $this->widget('application.widgets.iconcontrol.IconControl',
            array('icons'=>array(
                    'new_details'=>array('icon'=>'ui-icon-closethick','click'=>'function(){$(this).parent();}','title'=>'Add new courier reference'),
                    ),
            ));?>
    <div class="row">
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]marking"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]marking",array('maxlength'=>15)); ?>
            <?php echo CHtml::error($item,"[$i]marking"); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]pkg"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]pkg"); ?>
            <?php echo CHtml::error($item,"[$i]pkg"); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]wt"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]wt"); ?>
            <?php echo CHtml::error($item,"[$i]wt"); ?>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]width"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]width"); ?>
            <?php echo CHtml::error($item,"[$i]width"); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]length"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]length"); ?>
            <?php echo CHtml::error($item,"[$i]length"); ?>
        </div>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]height"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]height"); ?>
            <?php echo CHtml::error($item,"[$i]height"); ?>
        </div>
    </div>
    <?php if($editpackage):?>
    <div id="package_do_list" class="row">
        <?php foreach($item->do_list as $g=>$do_list): ?>
        <div class="col">
            <?php echo CHtml::activeLabelEx($item,"[$i]do_list[$g]"); ?>
            <?php echo CHtml::activeTextField($item,"[$i]do_list[$g]",array('value'=>$do_list)); ?>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>
<?php
$cs = Yii::app()->getClientScript();
$script =<<<EOD
$('body').data('package',0);
$('div.package input').live('change',function(){
    g=false;
    $("div.package:last input").each(function(){
    if($(this).val() != ""){
        g=true;
    }
    });
    if(g==true){
        var i=$('body').data('package')+1;
        $('body').data('package',i);
        $('#package_collections').append('<hr>');
        $('div.package:first').clone().appendTo('#package_collections');
        $("div.package:last").html($("div.package:last").html().replace(/\[[0-9]+\]/gi,'['+i+']'));
        $("div.package:last input").val("");
        $("div.package:last input").removeClass("error");
        $("div.package:last>div.row:last").data('index',0);
        $("div.package:last>div#package_do_list>div.col:gt(0)").remove();
        $("div.package:last>div#package_do_list input").attr('name','packing_list_packages['+i+'][do_list][0]');
    }
});
EOD;
if($editpackage) {
    $script .=<<<EOD
$('div#package_do_list input').live('keypress',function(){
    g=false;
    main = $(this).parent('div.col').parent('div#package_do_list');
    $(main).find("input").each(function()
    {
        if($(this).val() =="")
        {
            g=true;
        }
    }
    );

    if(g==false) {
        var i = $(main).data('index')+1;
        $(main).data('index',i);
        $(this).parent('div.col').clone().appendTo(main);
        $(main).children('div.col:last').children('input').val('');
        $(main).children('div.col:last').children('input').each(function(){
                var name = $(this).attr("name").match(/\[[\d\w]+\]/gi);
                var oldName='',newName = '';
                for(g=0;g<name.length;g++) {
                    oldName += name[g];
                }
                name[2] = "["+i+"]";
                for(g=0;g<name.length;g++) {
                    newName += name[g];
                }
                $(this).attr('name',$(this).attr('name').replace(oldName,newName));
                //$(this).parent('div').children('select').attr('name',$(this).parent('div').children('select').attr('name').replace(oldName,newName));
            });
    }
}
);
EOD;
}
$cs->registerScript("packageScript",$script,CClientScript::POS_READY);
?>